<?php 
namespace app\Home\controller;
use think\Controller;
use app\Home\model\Category;
use app\Home\model\Gs;
use think\Db;


class Index extends Common
{
	/**
     * 前台首页
	 */
	public function index(){
		//随机显示十条最热商品 用于热门推荐展示
        $data = $this->headerData();
		$data['hot'] = Db::query("select * from goods where is_hot=1 order by rand() limit 5");
		$cate  = Db('category')->select();//查询所有的分类
        //获取楼层数据
        $data['one']=$this->getFloorData(0,$cate);
        $data['two']=$this->getFloorData(1,$cate);
        $data['three']=$this->getFloorData(2,$cate);
        $data['photo'] = Db('photo')->select();
		$this->assign('data',$data);
		return $this->fetch();
	}

	/**
     * 获取楼层数据
     * @param  [int] $num [楼层数-1]
     * @param  [array] $cate [所有的分类数据]
     * @return [array]      [楼层数据]
     */
    public function getFloorData($num,$cate){
        $floor=array(3,4,5);//楼层分类排序
        $cateInfo=$this->getList($cate,$floor[$num]);//楼层所属分类
        $cateData=Db('category')->where('cat_id',$floor[$num])->select();
        $data['cat_name'] = $cateData[0]['cat_name'];
        $cat_id=[];
        foreach ($cateInfo as $key => $value) {
            $cat_id[]=$value['cat_id'];//获取所有分类id
            if ($value['parent_id']==$num) {
                $data['son_cate'][]=$value;//获取子集分类
            }   
        }
        $where['cat_id']=array('in',$cat_id);
        $where['is_sale']=1;
        $where['goods_number']=array('gt',0);
        $where['is_delete'] = 0;
        //根据分类id获取商品数据
        $data['goods']=Db('goods')->where($where)->limit(8)->select();
        return $data;	
    }

	/**
	 * 获取分类子集
	 * @param   [string] $data[分类数据]
	 * @param   [string] $parent_id[父集ID]
	 * @return [array] $newdata
	 */
	
	public function getList($data,$parent_id){
        $newdata = array();
        foreach ($data as $k => $v) {
            if($v['parent_id']==$parent_id){
                $newdata[]=$v;
                $this->getList($newdata,$v['cat_id']);
            }
        }
        return $newdata;
    }

	
}